package com.atung.merchant.result;

import com.atung.merchant.exception.UdpAppException;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author Administrator
 */
@Data
@JsonNaming(PropertyNamingStrategies.LowerCamelCaseStrategy.class)
public class ApiResult<T> implements Serializable {
    private int code;
    private String message;
    private T data;

    private ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private ApiResult(CommonResultCode crc, T data) {
        this.code = crc.getCode();
        this.message = crc.getMessage();
        this.data = data;
    }

    public static <T> ApiResult<T> ok() {
        return (ApiResult<T>) ok((Object) null);
    }

    public static <T> ApiResult<T> ok(T data) {
        return new ApiResult(CommonResultCode.SUCCESS, data);
    }


    public static <T> ApiResult<T> warn(String message) {
        return (ApiResult<T>) warn(CommonResultCode.WARN.getCode(), message, (Object) null);
    }

    public static <T> ApiResult<T> warn(int code, String message) {
        return (ApiResult<T>) warn(code, message, (Object) null);
    }

    public static <T> ApiResult<T> warn(T data) {
        return warn(CommonResultCode.WARN.getCode(), CommonResultCode.WARN.getMessage(), data);
    }

    public static <T> ApiResult<T> warn(int code, String message, T data) {
        return new ApiResult(code, message, data);
    }



    public static <T> ApiResult<T> fail() {
        return fail(CommonResultCode.FAIL.getMessage());
    }

    public static <T> ApiResult<T> fail(String message) {
        return fail(CommonResultCode.FAIL.getCode(), message);
    }
    public static <T> ApiResult<T> noAuth( ) {
        return fail(CommonResultCode.GIT_TOKEN_EXPIRE.getCode(), CommonResultCode.GIT_TOKEN_EXPIRE.getMessage());
    }

    public static <T> ApiResult<T>noLoginName( ) {
        return fail(CommonResultCode.LOGIN_ERR_USER_NAME_ISNULL.getCode(), CommonResultCode.LOGIN_ERR_USER_NAME_ISNULL.getMessage());
    }
    public static <T> ApiResult<T> pwdLoginErr( ) {
        return fail(CommonResultCode.LOGIN_ERR_USER_PWD_ISNULL.getCode(), CommonResultCode.LOGIN_ERR_USER_PWD_ISNULL.getMessage());
    }
    public static <T> ApiResult<T> noPermission( ) {
        return fail(CommonResultCode.NOT_PERMISSION.getCode(), CommonResultCode.NOT_PERMISSION.getMessage());
    }
    public static <T> ApiResult<T> requestControl( ) {
        return fail(CommonResultCode.REQUEST_FREQUENTLY.getCode(), CommonResultCode.REQUEST_FREQUENTLY.getMessage());
    }

    public static <T> ApiResult<T> requestIPControl( ) {
        return fail(CommonResultCode.REQUEST_FREQUENTLY.getCode(), CommonResultCode.REQUEST_FREQUENTLY.getMessage());
    }
    public static <T> ApiResult<T> fail(int code, String message) {
        return new ApiResult(code, message, (Object) null);
    }

    public static <T> ApiResult<T> compare(int condition, String failMsg, T data) {
        return compare(condition > 0, failMsg, data);
    }

    public static <T> ApiResult<T> compare(boolean condition, String failMsg, T data) {
        return condition ? ok(data) : fail(failMsg);
    }

    public void ensureOk(String errorMsg) {
        if (this.getCode() != 0) {
            throw new UdpAppException(errorMsg + "," + this.getMessage(), new Object[0]);
        }
    }

    public void ensureOk() {
        if (this.getCode() != 0) {
            throw new UdpAppException(this.getMessage(), new Object[0]);
        }
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public void setData(final T data) {
        this.data = data;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof ApiResult;
    }




}
